package com.general.framework.core.lang;

/**
 * @author leejean
 * @ClassName: Sensitives
 * @Description: 敏感信息处理工具类
 * @date 2020年10月14 11:19:22
 */
public class Sensitives {

    /**
     * 手机号脱敏
     *
     * @param mobile
     * @return
     */
    public static String mobile(final String mobile) {
        final int mobile_length = 11;
        if (Strings.isBlank(mobile) || mobile.length() < mobile_length) {
            return mobile;
        }
        return mobile.substring(0, 3) + "****" + mobile.substring(7);
    }

    /**
     * 中文信息脱敏
     *
     * @param userName
     * @return
     */
    public static String chineseName(final String userName) {
        if (Strings.isBlank(userName)) {
            return userName;
        }
        final int length = userName.length();
        if (length == 1) {
            return userName;
        } else if(length == 2){
            return userName.substring(0, 1).concat("*");
        } else {
            return userName.substring(0, 1).concat("*").concat(userName.substring(length - 1));
        }
    }

    /**
     * 地址脱敏
     *
     * @param address
     * @return
     */
    public static String address(final String address) {
        if (Strings.isBlank(address)) {
            return address;
        }
        final int length = 8, addressLen = address.length();
        if (addressLen < length) {
            return address;
        }
        return Strings.left(address, length) + "********";
    }

    /**
     * 订单收货人脱敏
     *
     * @param addressUser
     * @return
     */
    public static String addressUser(final String addressUser) {
        if (Strings.isBlank(addressUser)) {
            return addressUser;
        }
        final String splitChar = "  ";
        final int splitIndex = addressUser.indexOf(splitChar);
        if (splitIndex < 0) {
            return address(addressUser);
        }
        final String left = chineseName(addressUser.substring(0, addressUser.indexOf(splitChar)));
        final String right = mobile(addressUser.substring(splitIndex + splitChar.length()));
        return left + splitChar + right;
    }

    /**
     * [身份证号] 前六位，后四位，其他用星号隐藏每位1个星号<例子:451002********1647>
     *
     * @param idCard
     * @return
     */
    public static String idCard(final String idCard) {
        if (Strings.isBlank(idCard)) {
            return idCard;
        }
        return Strings.left(idCard, 6).concat("********").concat(Strings.right(idCard, 4));
    }

    /**
     * [银行卡号] 前六位，后四位，其他用星号隐藏每位1个星号<例子:6222600**********1234>
     *
     * @param cardNum
     * @return
     */
    public static String bankCard(final String cardNum) {
        if (Strings.isBlank(cardNum)) {
            return cardNum;
        }
        return Strings.left(cardNum, 6).concat(Strings.removeStart(Strings.leftPad(Strings.right(cardNum, 4), Strings.length(cardNum), "*"), "******"));
    }

}
